<?php
namespace Home\Controller;
use Think\Controller;
class JieController extends Controller {
    //添加帖子
    public function add(){
    	// 有提交，处理数据
    	if(IS_POST){
            $uid = $_SESSION['id'];
    		$title = $_POST['title'];
    		$content = $_POST['content'];
            $create_time = time();
    		$sql = "INSERT INTO question(uid,title,content,create_time) values('$uid','$title','$content','$create_time')";
    		
    		$res = M()->execute($sql);

    		if($res==1){              
               header('Location:'.U('home/index/index'));
    		}else{

    		}


    	}
    	// 没有提交，渲染模板
    	else{

	        //链接数据库 M();  取出所有数据  使用find()方法
	        $user = M('user')->find($_SESSION['id']);   //通过id找到对应的用户信息
	        $this->assign('user',$user); 
	        $this->display();
        }


    }

    //帖子详情
    public function detail(){
        //显示头像
        $user = M('user')->find($_SESSION['id']);   
        $this->assign('user',$user); 


        $id = $_GET['id'];

        //查找用户信息,问题标题，内容，浏览量等信息
        $sql = "SELECT u.face,u.nickname,u.id as uid ,  #用户信息
                       q.title,q.content,q.answer_num,q.view_num,q.id AS qid,q.status,q.create_time
                FROM user AS u,  #找用户信息,
                     question AS q #找问题内容
                WHERE u.id = q.uid  #表关联
                AND   q.id = '$id'  #定位当前要查找的问题 

        ";
        $res = M()->query($sql);
        // execute  这个是返回影响行数

        // 通过问题id qid 在answer表中查找所有答案
        // 并查找出回答者的资料 根据answer表的uid进行联表查询
        $sqls = " SELECT t2.qid,t2.id,t2.content,t3.nickname,t2.zan_num,t3.face,t3.id as uid,t2.create_time
                    FROM question AS t1 ,answer AS t2 ,user AS t3
                    WHERE t1.id=t2.qid
                    AND t1.id = '$id'
                    AND t3.id = t2.uid 
                ";

        $result = M()->query($sqls);
        //var_dump($result);


        // 用户每访问一次本页面，就让浏览量自增1
        $view ="UPDATE question SET view_num=(view_num+1) WHERE id='$id'";
        $views = M()->execute($view);



        //最近热帖
        $hots = "SELECT title,view_num,id FROM question 
                        ORDER BY view_num DESC,   #首要条件
                                  answer_num DESC  #次要条件
                    ";
        $hot =  M()->query($hots);
        $this->assign('hot',$hot);

        //最近热议
        $talks = "SELECT title,answer_num,id FROM question ORDER BY answer_num DESC,view_num DESC";
        $talk=  M()->query($talks);
        $this->assign('talk',$talk);




        $this->assign('res',$res[0]);
        //注入模板  ，循环输出相关信息
        $this->assign('result',$result);

        $this->display();
    }


    //提交回答    回答问题的数据提交到这个方法
    public function answer(){

        // 把回答的数据插入到数据库里  跳转回到detail页面，答案就刷新了

        // 插入答案，关键字段？ 
        // content  $_POST['content']
        // qid      $_POST['qid']
        // uid      当前登录用户的id  $_SESSION['id']
        // create_time 创建时间   time()
        
     
        $content = $_POST['content'];
        $qid = $_POST['qid'];
        $uid = $_SESSION['id'];
        $create_time = time();


        print_r($_POST);


        $sql = "INSERT INTO answer(content,qid,uid,create_time) VALUES('$content','$qid','$uid','$create_time')";

        $ans = M()->execute($sql);

        if($ans==0){
            echo '执行失败';exit;
        }

        // 更改问题的状态
        // 更改问题回答的次数，更改question 表里的 anser_num次数 
        if($ans>0){
            $sql = "UPDATE question SET answer_num= (answer_num+1) WHERE id='$qid'";        
            $ans = M()->execute($sql);

            //改变用户回答量
            $ans_num = "UPDATE user SET answer_num= (answer_num+1) WHERE id='$uid'";
            $ans_num = M()->execute($ans_num);
        }

       
        
        header('Location:'.U('home/jie/detail',array('id'=>$qid)));

    }

    // 处理点赞
    public function zan(){
        $id = $_GET['id'];
        $qid = $_GET['qid'];
        //判断用户是否登录,没登陆不能点赞，跳转到详情页
        if(empty($_SESSION['id'])){
            header('Location:'.U('detail',array('id'=>$qid)));
            // $this->error('您未登陆，不能点赞');
            return;
        }


        $uid = $_SESSION['id'];
        // 用户是否赞过？s
        // 查找当前用户（登陆的用户）  是否赞过提交过来的答案 （aid）
        $sql = "SELECT * FROM zan WHERE uid = '$uid' AND aid = '$id' ";
       
        $zan = M()->query($sql);

        // 如果数组为空，说明用户没有对该问题进行点赞过，或者点过赞，但是取消了
        if(empty($zan)){
            // 如果没赞过，插入一条数据到zan表，并且答案赞的总数+1
            // 让这条答案的赞+1
            $sql = "UPDATE answer SET zan_num = zan_num+1 WHERE id = '$id' ";
            $res = M()->execute($sql);
            // 把赞的记录写入到zan表里
            $aid = $id;
            $create_time = time();
            $sql = "INSERT INTO zan (uid,aid,create_time) VALUES('$uid','$aid','$create_time')";
            $res = M()->execute($sql);
        }
        // 如果用户赞过，那就取消它的赞，并且答案的总的赞数量-1
        else{

            $zan = $zan[0];
            //取消点赞
            $sql = "DELETE FROM zan WHERE id='{$zan['id']}'";
            $res = M()->execute($sql);
           
            //取消点赞后，点赞数量减1
            $sql = "UPDATE answer SET zan_num = zan_num-1 WHERE id = '$id' ";
            $res = M()->execute($sql);
        }

        header('Location:'.U('detail',array('id'=>$qid)));
    }

}